package com.yf.template;


import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.File;
import java.io.FileWriter;
import java.util.Properties;

public class TestJava {

    public static void main(String[] args) {
        Properties p = new Properties();
        p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        p.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
        Velocity.init(p);
        // 创建模版上下文对象
        VelocityContext context = new VelocityContext();
        context.put("className", "Test");
        // 模版对象
        Template template = Velocity.getTemplate("template/Java.java.vm", "UTF-8");
        // 执行模版合并
        File file = new File("src/main/java/com/yf/Test.java");
        try (FileWriter writer = new FileWriter(file)) {
            template.merge(context, writer);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
